﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static Dictionary<string, string> infoDic = new Dictionary<string, string>();
        static void Main(string[] args)
        {
            var strContent = File.ReadAllText("E:\\1.txt", Encoding.UTF8);
            ParseDatatable(strContent);

            Console.WriteLine("------------------解析后内容-------------------------");
            foreach (var item in infoDic)
            {
                Console.WriteLine(item.Key+" == "+item.Value);
            }
            Console.Read();
        }

        public static Dictionary<string, string> ParseDatatable(string msg)
        {
            bool isKey = false;
            bool isValue = false;
            bool isValueStart = false;//是否value首次检测
            int equalIndex=0;// = 出现次数的计数
            int valueStartIndex = 0;//Value的起始索引
            StringBuilder sbKey = new StringBuilder();
            StringBuilder sbvalue = new StringBuilder();

            for (int i = 0; i < msg.Length; i++)
            {
                switch (msg[i])
                {
                    case '#':
                        isKey = false;
                        if(isValue)//收集颜色码中的#
                            sbvalue.Append(msg[i]); 
                        break;
                    case '\r':
                        continue;
                    case '\n':
                        isKey = true;
                        isValue = false;
                        if (!string.IsNullOrEmpty(sbKey.ToString()) && !string.IsNullOrEmpty(sbvalue.ToString()))
                        {
                            //Console.WriteLine("key="+sbKey +" value="+sbvalue);
                            if (infoDic.ContainsKey(sbKey.ToString()))
                                Console.WriteLine("[ERROR]:has the same key:{0}, value:{1}", sbKey.ToString(), sbvalue.ToString());
                            else
                                infoDic.Add(sbKey.ToString(), sbvalue.ToString());
                        }
                        sbKey.Remove(0, sbKey.Length);
                        sbvalue.Remove(0, sbvalue.Length);
                        break;
                    case '=':
                        if(!isValue)//忽略value里的 = 计数
                            equalIndex++;
                        if (equalIndex % 2 == 0 && equalIndex >1)//key end
                        {
                            isKey = false;
                            isValue = true;
                            if (valueStartIndex != equalIndex)
                            {
                                isValueStart = true;
                                valueStartIndex = equalIndex;
                            }
                        }

                        if (isValue)
                        {
                            if (isValueStart && msg[i - 1] == '=')//忽略==value前最开始的那个=
                            {
                                isValueStart = false;
                                continue;
                            }
                            sbvalue.Append(msg[i]);
                        }
                        break;
                    default:
                        if (isKey)
                            sbKey.Append(msg[i]);
                        else if (isValue)
                        {
                            sbvalue.Append(msg[i]);
                            DealEndLine((i == msg.Length - 1), ref infoDic, sbKey, sbvalue);
                        }
                        break;
                }
            }
            return infoDic;
        }

        /// <summary>
        /// 行尾特殊处理
        /// </summary>
        /// <param name="lastLine">是否最后一行</param>
        /// <param name="dictionary">infoDic</param>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static void DealEndLine(bool lastLine,ref Dictionary<string,string> dictionary,StringBuilder key,StringBuilder value)
        {
            if (lastLine)
            {
                if (infoDic.ContainsKey(key.ToString()))
                    Console.WriteLine("[ERROR]:has the same key:{0}, value:{1}", key.ToString(),value.ToString());
                else
                    infoDic.Add(key.ToString(), value.ToString());
            }
        }
    }
}
